<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>销售成本明细表</title>
</head>

<body>
<div id="SalesorgForm_toolbar" style="text-align: center;height: 160px;background: #fafafa;">
    <form id="SalesorgForm" name="SalesorgForm" role="form" method="post">
        <div style="float: top;width: 100%;margin-top: 10px;">
            <div style="margin: 5px;width: 100%">
                <label>组织名称</label>
                <input id="pkSalesorg" name="pkSalesorg" data-options="prompt:'查询必选项'" value="" style="width:350px"
                       required="required">
                <label style="margin-left: 35px">日期范围</label>
                <input id="SalesorgStartTime" name="startTime" class="easyui-datebox" data-options="prompt:'查询必选项'"
                       style="width:190px" required="required" editable="false"/> —
                <input id="SalesorgEndTime" name="endTime" class="easyui-datebox" data-options="prompt:'查询必选项'"
                       style="width:190px" required="required" editable="false"/>
            </div>
            <div style="margin-top: 10px">
                <label>物料编码</label>
                <input type="text" id="WLCode" name="WLCode"
                       onkeyup="this.value=this.value.replace(/[^\x00-\xff]/g, '')"
                       placeholder="请输入物料编码，多个物料编码以半角逗号隔开" style="width: 350px;height: 30px">
                <a href="#" id="getSalesorgKH" class="easyui-linkbutton"
                   style="width: 90px;height: 30px;margin-left: 35px">选择客户</a>
                <input id="KHCode" name="KHCode" type="text" style="width: 140px;height: 30px" placeholder="请点击选择客户"
                       disabled="true">
                <label style="margin-left: 85px;font-size: 16px">重新计算数据</label>
                <input type="checkbox" id="checkbox" name="checkbox" value="1"
                       style="vertical-align:middle; margin:auto;width: 18px;height:18px; padding:0"/>
            </div>
            <div id="Salesorg_dlg" class="easyui-dialog" closed="true" style="margin: 5px;width: 580px"
                 buttons="#Salesorg_dlgButtons">
                <div id="Salesorg_tb" style="padding:5px ;width: 100%">
                    <span>客户类型:</span>
                    <select id="itemid" style="width: 150px;height: 30px">
                        <option selected value="">--- 空 ---</option>
                        <option value="外省">外省</option>
                        <option value="内地客户">内地客户</option>
                        <option value="嘉利澳滋">嘉利澳滋</option>
                        <option value="直供场">直供场</option>
                        <option value="直供场喜羊羊">直供场喜羊羊</option>
                        <option value="内部客户">内部客户</option>
                        <option value="海外客户">海外客户</option>
                        <option value="港澳台客户">港澳台客户</option>
                        <option value="包场">包场</option>
                        <option value="代理商喜洋洋">代理商喜洋洋</option>
                        <option value="国产澳宝">国产澳宝</option>
                    </select>&nbsp;&nbsp;&nbsp;
                    <span>客户名称:</span>
                    <input id="productid" style="line-height:26px;border:1px solid #ccc;width: 180px">
                    <a href="#" class="easyui-linkbutton" onclick="doSearch()"
                       data-options="iconCls:'icon-search'">搜索</a>
                </div>
                <table id="Salesorg_tt" class="easyui-datagrid" style="width:100%;height:350px"
                       url='/salesorg/bdCustomer' toolbar="#Salesorg_tb" rownumbers="true"
                       idField="KHid" pagination="true"
                       data-options="pageSize: 20,pageList: [20,50,100],">
                    <thead>
                    <tr>
                        <th field="KHid" checkbox="true"></th>
                        <th field="typeName" width="200">客户类型</th>
                        <th field="name" width="300">客户名称</th>
                    </tr>
                    </thead>
                </table>
            </div>

        </div>
        <span th:if="${session.user} != null">
			<input id="user_Name" type="text" style="display: none;" th:value="${session.user.userName}">
		</span>
        <div style="margin-top: 20px;float: bottom;text-align: center">
            数据源<select id="dsName" class="easyui-combobox" name="dsName" style="width:100px;" data-options="panelHeight:'auto'">
            <option value="">澳宝集团</option>
            <option value="two">OPAL202012</option>
        </select>
            <a href="#" class="easyui-linkbutton" style="width: 120px;height: 35px" data-options="iconCls:'icon-save'"
               id="crop" onclick="OUT_excel()">导出excel</a>&nbsp
            <a href="#" class="easyui-linkbutton" style="width: 120px;height: 35px;margin-left: 10px"
               data-options="iconCls:'icon-search'"
               onclick="SalesorgCheck()"> 查询</a>
        </div>
    </form>
    <div id="Salesorg_dlgButtons">
        <a href="#" class="easyui-linkbutton" iconCls="icon-ok" onclick="save_Salesorg_dlg()">保存</a>
        <a href="#" class="easyui-linkbutton" iconCls="icon-cancel"
           onclick="javascript:$('#Salesorg_dlg').dialog('close')">取消</a>
    </div>
</div>
<div class="panel datagrid" style="width: 100%;height: 100%">
    <table id="Salesorg_table" class="easyui-datagrid" style="width: 100%;height: 100%"
           pagination="true" rownumbers="true" toolbar="#SalesorgForm_toolbar"
           data-options="pageSize: 50,pageList: [50,100,200],url: '/salesorg/check'"/>
</div>
<script th:inline="none" type="text/javascript">

    var Salesorg_ids = [];

    $(function () {
        //获取组织名称
        if ($("#user_Name").val() != null && $("#user_Name").val() != "") {
            $('#pkSalesorg').combobox({
                url: '/user/getUserOrgHrorg',
                valueField: 'pkhrorg',
                textField: 'name'
            });
        } else {
            warnSessionConfirm()
        }
    })

    //选择客户
    $("#getSalesorgKH").click(function () {
        $('#Salesorg_dlg').dialog('open').dialog('setTitle', '选择客户')
    })

    //保存选择客户
    function save_Salesorg_dlg() {
        Salesorg_ids = []
        var rows = $('#Salesorg_tt').datagrid('getSelections');
        for (var i = 0; i < rows.length; i++) {
            Salesorg_ids.push(rows[i].KHid);
        }
        $('#KHCode').val(Salesorg_ids.join(','));
        $('#Salesorg_dlg').dialog('close');
    }

    function OUT_excel() {
        var pkSalesorg = $("input[name=pkSalesorg]").val();
        var startTime = $("input[name=startTime]").val();
        var endTime = $("input[name=endTime]").val();
        var dsName = $("input[name=dsName]").val();
        var user_name = $("#user_Name").val();
        if (user_name != null && user_name != "") {
            if (pkSalesorg != "" && startTime != "" && endTime != "") {
                // 判断选择日期范围小于三个月
                var year = startTime.substring(0, 4);
                var year1 = endTime.substring(0, 4);
                var month = startTime.substring(5, 7);
                var month2 = endTime.substring(5, 7);
                if (year == year1 && month2 - month <= 2) {
                    // 监听导出结束
                    listenEnd();
                    $("#crop").css("color", "#CCC");
                    $('#crop').linkbutton('disable');
                    if ($('#checkbox').is(':checked')) {
                        //等待层
                        window.location.href = "/salesorg/excel_down?pkSalesorg="+pkSalesorg+"&startTime="+startTime+"&endTime="+endTime+"&checkbox=1&dsName="+dsName;
                       await("请稍等....执行重新计算并导出excel  选择日期范围越大，导出时间会越长!(请勿关闭页面)");
                    } else {
                        window.location.href = "/salesorg/excel_down?pkSalesorg="+pkSalesorg+"&startTime="+startTime+"&endTime="+endTime+"&dsName="+dsName;
                        await("请稍等....正在导出excel  选择日期范围越大，导出时间会越长!(请勿关闭页面)");
                    }
                } else {
                    layer.msg("请输入三个月以内的日期范围，三个月以上请分批导出！")
                }
            } else {
                layer.msg("请填写好信息!（组织名称，日期是必选项！)")
            }
        } else {
            warnSessionConfirm();
        }
    };


    function SalesorgCheck() {
        var pkSalesorg = $("input[name=pkSalesorg]").val();
        var startTime = $("input[name=startTime]").val();
        var endTime = $("input[name=endTime]").val();
        var user_name = $("#user_Name").val();
        if (user_name != null && user_name != "") {
            if (pkSalesorg != "" && startTime != "" && endTime != "") {
                var Salesorg_table_val={
                    pkSalesorg: $('#pkSalesorg').val(),
                    startTime: $('#SalesorgStartTime').val(),
                    endTime: $('#SalesorgEndTime').val(),
                    checkbox: $('#checkbox').is(':checked'),
                    KHCode: $('#KHCode').val(),
                    WLCode: $('#WLCode').val(),
                    dsName:$('#dsName').val()
                }

                if ($('#checkbox').is(':checked')) {
                    $('#Salesorg_table').datagrid({
                        queryParams:Salesorg_table_val,
                        onLoadSuccess: function (data) {
                            if (data == null || data.total == 0) {
                                layer.msg("没有相关的数据!")
                                $('#Salesorg_table').closest('div.datagrid-wrap').find('div.datagrid-pager').hide();
                            } else if ($('#checkbox').is(':checked')) {
                                topCommonMessage("成功执行重新计算!")
                            }
                        },
                        columns: [[
                            {field: 'a01', title: '销售组织', width: 60, align: 'center'},
                            {field: 'a02', title: '销售组织名称', width: 175, align: 'center'},
                            {field: 'a03', title: '单据类型', width: 60, align: 'center'},
                            {field: 'a04', title: '订单号', width: 135, align: 'center'},
                            {field: 'a05', title: '部门', width: 120, align: 'center'},
                            {field: 'a06', title: '客户编码', width: 60, align: 'center'},
                            {field: 'a07', title: '客户名称', width: 150, align: 'center'},
                            {field: 'a08', title: '库存组织', width: 60, align: 'center'},
                            {field: 'a09', title: '库存组织名称', width: 175, align: 'center'},
                            {field: 'a10', title: '出库单号', width: 120, align: 'center'},
                            {field: 'a11', title: '备注', width: 60, align: 'center'},
                            {field: 'a12', title: '仓库编码', width: 60, align: 'center'},
                            {field: 'a13', title: '仓库名称', width: 70, align: 'center'},
                            {field: 'a14', title: '行号', width: 60, align: 'center'},
                            {field: 'a15', title: '物料编码', width: 95, align: 'center'},
                            {field: 'a16', title: '物料名称', width: 200, align: 'center'},
                            {field: 'a17', title: '规格', width: 60, align: 'center'},
                            {field: 'a18', title: '型号', width: 120, align: 'center'},
                            {field: 'a19', title: '出库日期', width: 90, align: 'center'},
                            {field: 'a20', title: '实发主数量', width: 65, align: 'center'},
                            {field: 'a21', title: '原币币种', width: 80, align: 'center'},
                            {field: 'a22', title: '折本汇率', width: 80, align: 'center'},
                            {field: 'a23', title: '原币单价', width: 80, align: 'center'},
                            {field: 'a24', title: '原币金额', width: 80, align: 'center'},
                            {field: 'a25', title: '主本币无税单价', width: 80, align: 'center'},
                            {field: 'a26', title: '主本币含税单价', width: 80, align: 'center'},
                            {field: 'a27', title: '本币无税金额', width: 80, align: 'center'},
                            {field: 'a28', title: '本币价税合计', width: 80, align: 'center'},
                            {field: 'a29', title: '本币税额', width: 80, align: 'center'},
                            {field: 'a30', title: '成本金额', width: 80, align: 'center'},
                            {field: 'a31', title: '材料金额', width: 80, align: 'center'},
                            {field: 'a32', title: '成本单价', width: 80, align: 'center'},
                            {field: 'a33', title: '材料单价', width: 80, align: 'center'},
                            {field: 'a34', title: '人工', width: 80, align: 'center'},
                            {field: 'a35', title: '制费', width: 80, align: 'center'},
                            {field: 'a36', title: '加工费', width: 80, align: 'center'},
                            {field: 'a37', title: '客料转自订料', width: 100, align: 'center'},
                            {field: 'a38', title: '客人指定供应商', width: 80, align: 'center'},
                            {field: 'a39', title: '非客料', width: 100, align: 'center'},
                            {field: 'a40', title: '原料成本', width: 100, align: 'center'},
                            {field: 'a41', title: '配件成本', width: 80, align: 'center'},
                            {field: 'a42', title: '水料成本', width: 80, align: 'center'},
                            {field: 'a43', title: '半成品成本', width: 100, align: 'center'},
                            {field: 'a44', title: '成品', width: 80, align: 'center'},
                            {field: 'a45', title: '归并号', width: 100, align: 'center'},
                            {field: 'a46', title: '单位重量', width: 80, align: 'center'},
                            {field: 'a47', title: '实际重量', width: 80, align: 'center'},
                            {field: 'a48', title: '内购原材料单价', width: 100, align: 'center'},
                            {field: 'a49', title: '外购原材料单价', width: 100, align: 'center'},
                            {field: 'a50', title: '成本单价非存货', width: 100, align: 'center'},
                            {field: 'a51', title: '成本金额非存货', width: 100, align: 'center'},
                            {field: 'a52', title: '赠品', width: 80, align: 'center'},
                            {field: 'a53', title: '物料基本分类', width: 100, align: 'center'},
                            {field: 'a54', title: '物料分类', width: 80, align: 'center'},
                            {field: 'a55', title: '是否H技术产品', width: 100, align: 'center'},
                            {field: 'a56', title: '水料公斤数', width: 80, align: 'center'},
                            {field: 'a57', title: '订单行备注', width: 80, align: 'center'},
                            {field: 'a58', title: '购买类别', width: 80, align: 'center'},
                        ]]
                    });
                } else
                    $('#Salesorg_table').datagrid({
                        queryParams:Salesorg_table_val,
                        onLoadSuccess: function (data) {
                            if (data == null || data.total == 0) {
                                layer.msg("没有相关的数据!")
                                $('#Salesorg_table').closest('div.datagrid-wrap').find('div.datagrid-pager').hide();
                            }
                        },
                        columns: [[
                            {field: 'a01', title: '销售组织', width: 60, align: 'center'},
                            {field: 'a02', title: '销售组织名称', width: 175, align: 'center'},
                            {field: 'a03', title: '单据类型', width: 60, align: 'center'},
                            {field: 'a04', title: '订单号', width: 135, align: 'center'},
                            {field: 'a05', title: '销售部门', width: 130, align: 'center'},
                            {field: 'a06', title: '客户编码', width: 60, align: 'center'},
                            {field: 'a07', title: '客户名称', width: 150, align: 'center'},
                            {field: 'a08', title: '库存组织', width: 60, align: 'center'},
                            {field: 'a09', title: '库存组织名称', width: 175, align: 'center'},
                            {field: 'a10', title: '出库单号', width: 120, align: 'center'},
                            {field: 'a11', title: '备注', width: 60, align: 'center'},
                            {field: 'a12', title: '仓库编码', width: 60, align: 'center'},
                            {field: 'a13', title: '仓库名称', width: 70, align: 'center'},
                            {field: 'a14', title: '行号', width: 60, align: 'center'},
                            {field: 'a15', title: '物料编码', width: 95, align: 'center'},
                            {field: 'a16', title: '物料名称', width: 200, align: 'center'},
                            {field: 'a17', title: '规格', width: 60, align: 'center'},
                            {field: 'a18', title: '型号', width: 120, align: 'center'},
                            {field: 'a19', title: '出库日期', width: 90, align: 'center'},
                            {field: 'a20', title: '实发主数量', width: 65, align: 'center'},
                            {field: 'a21', title: '主本币无税单价', width: 100, align: 'center'},
                            {field: 'a22', title: '主本币含税单价', width: 100, align: 'center'},
                            {field: 'a23', title: '本币无税金额', width: 80, align: 'center'},
                            {field: 'a24', title: '本币价税合计', width: 80, align: 'center'},
                            {field: 'a25', title: '本币税额', width: 80, align: 'center'},
                            {field: 'a26', title: '成本金额', width: 80, align: 'center'},
                            {field: 'a27', title: '材料金额', width: 80, align: 'center'},
                            {field: 'a28', title: '成本单价', width: 80, align: 'center'},
                            {field: 'a29', title: '材料单价', width: 80, align: 'center'},
                            {field: 'a30', title: '人工', width: 80, align: 'center'},
                            {field: 'a31', title: '制费', width: 80, align: 'center'},
                            {field: 'a32', title: '加工费', width: 80, align: 'center'},
                            {field: 'a33', title: '结算币别', width: 80, align: 'center'},
                            {field: 'a34', title: '折本汇率', width: 80, align: 'center'},
                            {field: 'a35', title: '原币主含税单价', width: 100, align: 'center'},
                            {field: 'a36', title: '原币主无税单价', width: 100, align: 'center'},
                            {field: 'a37', title: '原币无税金额', width: 80, align: 'center'},
                            {field: 'a38', title: '原币价税合计', width: 80, align: 'center'},
                            {field: 'a39', title: '原币税额', width: 60, align: 'center'},
                            {field: 'a40', title: '客料转自订料', width: 100, align: 'center'},
                            {field: 'a41', title: '客人指定供应商', width: 100, align: 'center'},
                            {field: 'a42', title: '非客料', width: 80, align: 'center'},
                            {field: 'a43', title: '原料成本', width: 80, align: 'center'},
                            {field: 'a44', title: '配件成本', width: 80, align: 'center'},
                            {field: 'a45', title: '水料成本', width: 80, align: 'center'},
                            {field: 'a46', title: '半成品', width: 80, align: 'center'},
                            {field: 'a47', title: '成品', width: 60, align: 'center'},
                            {field: 'a48', title: '归并号', width: 80, align: 'center'},
                            {field: 'a49', title: '单位重量', width: 80, align: 'center'},
                            {field: 'a60', title: '实际重量', width: 80, align: 'center'},
                            {field: 'a51', title: '内购原材料单价', width: 80, align: 'center'},
                            {field: 'a52', title: '外购原材料单价', width: 80, align: 'center'},
                            {field: 'a53', title: '成本单价非存货', width: 80, align: 'center'},
                            {field: 'a54', title: '成本金额非存货', width: 80, align: 'center'},
                            {field: 'a55', title: '赠品', width: 60, align: 'center'},
                            {field: 'a56', title: '物料基本分类', width: 100, align: 'center'},
                            {field: 'a57', title: '物料分类', width: 60, align: 'center'},
                            {field: 'a58', title: '是否H技术产品', width: 100, align: 'center'},
                            {field: 'a59', title: '水料公斤数', width: 80, align: 'center'},
                            {field: 'a60', title: '订单行备注', width: 80, align: 'center'},
                            {field: 'a61', title: '购买类别', width: 80, align: 'center'},
                        ]]
                    });
            } else {
                layer.msg("请填写好信息!（组织名称，日期是必选项！)")
            }
        } else {
            warnSessionConfirm()
        }
    };


    var txtendflag;

    //监听结束
    function listenEnd() {//定时监听
        var loop = setInterval(function () {
            if (txtendflag == "1") {
                clearInterval(loop);//停止定时任务
                $(".datagrid-mask").remove();//解除遮罩
                $(".datagrid-mask-msg").remove();//关闭加载中遮罩
                $("#crop").linkbutton("enable");
                $("#crop").css("color", "black");//解除导出按钮
                txtendflag = "";
            } else {
                getendflag();
            }
        }, 3000);//单位毫秒  注意：如果导出页面很慢时，建议循环时间段稍长一点
    }

    // 监听导出excel结束返回值
    function getendflag() {//请求session标记位
        $.ajax({
            type: 'post',
            url: '/salesorg/getendflag',
            dataType: 'json',
            success: function (data) {
                txtendflag = data.flag;
            },
            error: function (error) {
                console.log('接口不通' + error);
            }
        })
    }


    //等待层
    function await(value) {
        $("<div class=\"datagrid-mask\"></div>").css({
            display: "block",
            width: "100%",
            height: "auto"
        }).appendTo("body");
        $("<div class=\"datagrid-mask-msg\"></div>").html("<img  class ='img1' />"+value).appendTo("body").css({
            display: "block",
            'font-size': '12px',
            height: "auto",
            left: ($(document.body).outerWidth(true) - 300) / 2,
            top: ($(window).height() + 100) / 2
        });
    }


    function doSearch() {
        $('#Salesorg_tt').datagrid('load', {
            typeName: $('#itemid').val(),
            name: $('#productid').val()
        });
    }

</script>
</body>
</html>